package org.iteam.xlz.manager.action.message;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.core.service.base.BaseParameterService;
import org.iteam.web.shiro.ShiroUtil;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.web.struts.action.RestAction;
import org.iteam.xlz.core.entity.messages.PushInfo;
import org.iteam.xlz.core.service.message.PushInfoService;
import org.iteam.xlz.manager.utils.Demo;

@SuppressWarnings("unchecked")
@Results({@Result(name="INDEX",type="freemarker",location="/WEB-INF/content/message/pushinfo/index.html"),
    @Result(name="EDIT",type="freemarker",location="/WEB-INF/content/message/pushinfo/edit.html"),
    @Result(name="SMS",type="freemarker",location="/WEB-INF/content/message/pushinfo/sms.html")})
public class PushInfoAction extends AbstractModuleAction<PushInfo> implements RestAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final String PATH="/message/pushinfo";
	@Override
	public PushInfo getModel() {
		if(null==model){
			model=new PushInfo();
		}
		return model;
	}

	@Override
	@Action(PATH+"/index")
	public String index() throws Exception {
		return INDEX;
	}
	
	@Override
	@Action(PATH+"/query")
	public String query(){
		map.put("total",PushInfoService.getInstance().counts());
		map.put("rows",PushInfoService.getInstance().query(page,rows));
		return MAP;
	}
	
	@Action("/message/sms/index")
	public String smsIndex() throws SQLException{
		map.put("ticketgivingsmstemplid",BaseParameterService.getInstance().getValueByName("ticketgivingsmstemplid"));
		return "SMS";
	}
	
	@Action("/message/sms/query")
	public void smsQuery() throws IOException{
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-type", "application/json;charset=UTF-8");
		map.clear();
		map.put("apikey","f2bec1bfea4e768a41ec02b9dc86f3aa");
		String res=Demo.post("https://sms.yunpian.com/v2/tpl/get.json",map);
		PrintWriter writer=super.response.getWriter();
		String result="{\"total\":100,\"rows\":"+res+"}";
		writer.print(result);
		writer.flush();
		writer.close();
	}

	@Override
	public String show() {

		return null;
	}

	@Override
	@Action(PATH+"/create")
	public String create() {
		model.setCreator(ShiroUtil.currentAccount());
		map.put("state",PushInfoService.getInstance().create(model));
		return MAP;
	}

	@Override
	@Action(PATH+"/update")
	public String update() {
		map.put("state",PushInfoService.getInstance().update(model));
		return MAP;
	}

	@Override
	public String delete() {

		return null;
	}

	@Override
	public String destroy() {
		return null;
	}

	@Override
	@Action(PATH+"/{_id}/edit")
	public String edit() throws Exception {
		super.object=PushInfoService.getInstance().findOne(_id);
		return EDIT;
	}

	@Override
	@Action(PATH+"/new")
	public String editNew() throws Exception {
		return EDIT;
	}

}
